package com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authority.starter.util.VerifyHelper;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.Element;
import com.smedi.ismedi.authoritymanagement.core.domain.dto.ElementQuery;
import com.smedi.ismedi.authoritymanagement.core.domain.repository.ElementRepository;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.model.ElementEntity;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.model.RoleElementRelationshipEntity;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.mybatis.mapper.ElementMapper;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.mybatis.mapper.RoleElementRelationshipMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 基础信息仓库实现类
 *
 * @author cookie
 * @date 2022/07/14
 */
@Repository
@AllArgsConstructor
public class ElementRepositoryImpl implements ElementRepository {
    private final AdapterConverter adapterConverter;
    private final ElementMapper elementMapper;
    private final RoleElementRelationshipMapper roleElementRelationshipMapper;

    @Override
    public Element saveElement(Element element) {
        ElementEntity elementEntity = adapterConverter.elementToElementEntity(element);
        if (ObjectUtils.isNull(elementEntity.getId())) {
            elementMapper.insert(elementEntity);
        }
        if (ObjectUtils.isNotNull(elementEntity.getId())) {
            elementMapper.updateById(elementEntity);
        }

        return adapterConverter.elementEntityToElement(elementEntity);
    }

    @Override
    public void deleteElement(ElementQuery elementQuery) {
        LambdaQueryWrapper<ElementEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(ObjectUtils.isNotNull(elementQuery.getId()), ElementEntity::getId, elementQuery.getId());
        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(elementQuery.getIds()), ElementEntity::getId, elementQuery.getIds());

        elementMapper.delete(lambdaQueryWrapper);

    }

    @Override
    public Element getElementOne(ElementQuery elementQuery) {
        LambdaQueryWrapper<ElementEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(ObjectUtils.isNotNull(elementQuery.getId()), ElementEntity::getId, elementQuery.getId());

        ElementEntity elementEntity = elementMapper.selectOne(lambdaQueryWrapper);

        return adapterConverter.elementEntityToElement(elementEntity);
    }

    @Override
    public List<Element> getElementList(ElementQuery elementQuery) {
        LambdaQueryWrapper<ElementEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        boolean hasAboutIdQuery = false;
        Set<Long> elementIds = new HashSet<>();
        if (CollectionUtils.isNotEmpty(elementQuery.getIds())) {
            elementIds.addAll(elementQuery.getIds());
            hasAboutIdQuery = true;
        }

        if (CollectionUtils.isNotEmpty(elementQuery.getRoleIds())) {
            LambdaQueryWrapper<RoleElementRelationshipEntity> roleElementRelationshipEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roleElementRelationshipEntityLambdaQueryWrapper.in(RoleElementRelationshipEntity::getRoleId, elementQuery.getRoleIds());
            List<RoleElementRelationshipEntity> roleElementRelationshipEntities = roleElementRelationshipMapper.selectList(roleElementRelationshipEntityLambdaQueryWrapper);
            if (CollectionUtils.isEmpty(roleElementRelationshipEntities)) {
                return new ArrayList<>();
            }
            if (CollectionUtils.isEmpty(elementIds)) {
                elementIds.addAll(roleElementRelationshipEntities.stream().map(RoleElementRelationshipEntity::getElementId).collect(Collectors.toList()));
            }
            if (CollectionUtils.isNotEmpty(elementIds)) {
                elementIds.retainAll(roleElementRelationshipEntities.stream().map(RoleElementRelationshipEntity::getElementId).collect(Collectors.toList()));
            }
            hasAboutIdQuery = true;
        }

        if (hasAboutIdQuery && elementIds.isEmpty()) {
            return new ArrayList<>();
        }

        lambdaQueryWrapper.in(VerifyHelper.isAvailable(elementIds), ElementEntity::getId, elementIds);
        lambdaQueryWrapper.eq(VerifyHelper.isAvailable(elementQuery.getInitial()), ElementEntity::getInitial, elementQuery.getInitial());

        List<ElementEntity> elementEntities = elementMapper.selectList(lambdaQueryWrapper);

        return adapterConverter.elementEntityToElement(elementEntities);
    }

    @Override
    public Page<Element> getElementPage(ElementQuery elementQuery) {
        Page<ElementEntity> elementEntityPage = new Page<>(elementQuery.getCurrent(), elementQuery.getSize());

        LambdaQueryWrapper<ElementEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.orderBy(true, false, ElementEntity::getId);

        Page<ElementEntity> elementEntityPageResult = elementMapper.selectPage(elementEntityPage, lambdaQueryWrapper);

        return adapterConverter.elementEntityPageToElementPage(elementEntityPageResult);
    }
}